import Toast from '@vant/weapp/toast/toast';
import {
  showModel,
  showMsg
} from '../../../utils/config'
var WXBizDataCrypt = require('../../../utils/WXBizDataCrypt') //解密手机号
import {
  reqWxUserInfo,
  reqBindWx,
  reqUnBindWx,
  reqBindMobile,
  reqOrgCount,
  reqGetPhoneNum
} from '../../../request/api.js'
const app = getApp()
const exitLoginBehavior = require('../../behaviors/personal/exitLogin.js')
Page({
  behaviors: [exitLoginBehavior],
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    username: '',
    nickname: '',
    mobile: '',
    avatar: '',
    wxNickName: '',
    wxAvatar: '',
    openId: '',

    innerShow: false, //协议....
  },


  onLoad: function () {
    this.getUserInfo()
  },
  onShow() {
    this.getTabBar().init(); //tabbar
    this.getUserInfo()

  },

  getUserInfo() {
    this.setData({
      username: app.globalData.username,
      nickname: app.globalData.nickname,
      mobile: app.globalData.mobile,
      avatar: app.globalData.avatar || wx.getStorageSync('avatar'),
      openId: app.globalData.openId,
    })
  },

  goHome() {
    wx.navigateTo({
      url: '/packageC/pages/family/family/family',
    })
  },

  // 去个人信息
  goUserInfo() {
    wx.navigateTo({
      url: '/pages/personal-inside-page/user-info/user-info',
    })
  },

  // 去单位切换
  orgToggleClick() {
    wx.navigateTo({
      url: `/pages/personal-inside-page/org/org?mark=personalCenter`,
    })
  },

  // 去重置密码
  goResetPwd() {
    wx.navigateTo({
      url: '/packageA/pages/personal-inside-page/reset-pwd/reset-pwd',
    })
  },

  goMoney() {
    wx.navigateTo({
      url: '/packageA/pages/personal-inside-page/money/money',
    })
  },

  goSubions() {
    wx.navigateTo({
      url: '/pages/personal-inside-page/subions/subions',
    })
  },

  /**
   * @name: 解绑/绑定微信操作
   * @msg:   根据openId判断
   * @return {*}
   */
  bindWxClick() {
    let openId = wx.getStorageSync('openId')
    if (openId) {
      // 解绑
      this.unBindWx()
    } else {
      // 绑定
      // showModel('温馨提示', '是否绑定微信？', async (e) => {
      //     if (e) {
      this.bindWx()
      // } else {
      //     console.log('绑定取消')
      // }
      // })
    }
  },
  /**
   * @name: 绑定微信
   * @msg: 
   * @return {*}
   */
  bindWx() {
    let that = this
    wx.showLoading({
      title: '绑定中...',
      mask: true
    })
    wx.getUserProfile({
      desc: '用于获取微信基本信息',
      success: async function (res) {
        let {
          nickName: wxNickName,
          avatarUrl: wxAvatar
        } = res.userInfo
        wx.login({
          async success(res) {
            if (res.code) {
              const res1 = await reqWxUserInfo({
                jsCode: res.code
              }) //获取信息jsCode
              if (res1.code !== 20000) {
                wx.hideLoading()
                showMsg(res1.message)
                return
              }
              let {
                row
              } = res1.data
              let params = {
                id: app.globalData.userId,
                openId: row.openid,
                unionid: row.unionid,
                wxNickName,
                wxAvatar
              }
              const res2 = await reqBindWx(params) //绑定微信
              if (res2.code !== 20000) {
                wx.hideLoading()
                Toast(res2.message)
                return
              }
              app.globalData.wxNickName = wxNickName
              wx.setStorageSync('wxNickName', wxNickName)
              app.globalData.wxAvatar = wxAvatar
              wx.setStorageSync('wxAvatar', wxAvatar)
              that.setData({
                wxNickName,
                wxAvatar
              })
              app.globalData.openId = row.openid
              wx.setStorageSync('openId', row.openid) //微信openId
              that.setData({
                openId: row.openid
              })
              wx.hideLoading()
              wx.showToast({
                title: '绑定成功',
              })
            } else {
              console.log('绑定失败！' + res.errMsg)
              wx.hideLoading()
            }
          }
        })
      },
      fail: function (err) {
        wx.hideLoading()
        // 需要弹出隐私协议
        this.setData({
          innerShow: true
        })
        console.log(err, '获取用户基本信息');
      }
    })
  },
  /**
   * @name: 解绑微信
   * @msg: 
   * @return {*}
   */
  unBindWx() {
    let that = this
    wx.login({
      async success(res) {
        if (res.code) {
          showModel('温馨提示', '是否解绑？', async (e) => {
            if (e) {
              let params = {
                jsCode: res.code,
                id: app.globalData.userId
              }
              const res1 = await reqUnBindWx(params)
              if (res1.code !== 20000) return Toast(res1.message)
              app.globalData.openId = ''
              wx.removeStorageSync('openId')
              app.globalData.wxNickName = ''
              wx.removeStorageSync('wxNickName')
              app.globalData.wxAvatar = ''
              wx.removeStorageSync('wxAvatar')
              that.setData({
                wxNickName: '',
                wxAvatar: '',
                openId: ''
              })
              wx.showToast({
                title: '解绑成功',
              })
            }
          })
        } else {
          console.log('解绑失败！' + res.errMsg)
        }
      }
    })
  },


  // 更改手机号
  onChangePhone() {
    wx.navigateTo({
      url: '/pages/bindPhone/bindPhone',
    })
  },

  // 去订阅信息
  goSubsubscrib() {
    wx.navigateTo({
      url: '/packageC/pages/personal-inside-page/subscribe/subscribe',
    })
  },

  // 去帮助
  goHelp() {
    wx.navigateTo({
      url: '/packageA/pages/personal-inside-page/help/help',
    })
  },

  // 去公众号
  goOfficeAccount() {
    wx.navigateTo({
      url: '/packageA/pages/personal-inside-page/office-account/office-account',
    })
  },


})